local xiaoerbianri = fk.CreateSkill {
  name = "lingling__xiaoerbianri",
  dynamic_desc = function (self, player, lang)
    if player:usedSkillTimes("lingling__youjiaowulei", Player.HistoryGame) == 0 then
        return "当你拼点时，你可以用牌堆顶的牌拼点。你的<font color='red'>♥</font>拼点牌视为K。"
    else
        return "当你拼点时，你可以用牌堆顶的牌拼点。"
    end
  end,
}

Fk:loadTranslationTable{
  ["lingling__xiaoerbianri"] = "小儿辩日",
  [":lingling__xiaoerbianri"] = "当你拼点时，你可以用牌堆顶的牌拼点。你的<font color='red'>♥</font>拼点牌视为K。",

  ["#lingling__xiaoerbianri-invoke"] = "小儿辩日：是否用牌堆顶牌拼点？",
}

xiaoerbianri:addEffect(fk.StartPindian, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xiaoerbianri.name) and (player == data.from or table.contains(data.tos, player))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = xiaoerbianri.name,
      prompt = "#lingling__xiaoerbianri-invoke"
    })
  end,
  on_use = function(self, event, target, player, data)
    if player == data.from then
      data.fromCard = Fk:getCardById(player.room.draw_pile[1])
    else
      data.results[player] = data.results[player] or {}
      data.results[player].toCard = Fk:getCardById(player.room.draw_pile[1])
    end
  end,
})

xiaoerbianri:addEffect(fk.PindianCardsDisplayed, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(xiaoerbianri.name) and player:usedSkillTimes("lingling__youjiaowulei", Player.HistoryGame) == 0 then
      if player == data.from and data.fromCard.suit == Card.Heart then
        return true
      elseif data.results[player] and data.results[player].toCard.suit == Card.Heart then
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:changePindianNumber(data, player, 13, xiaoerbianri.name)
  end,
})

xiaoerbianri:addTest(function (room, me)
  FkTest.runInRoom(function ()
    room:handleAddLoseSkills(me, xiaoerbianri.name)
  end)
  local comp2 = room.players[2]
  local card = room:printCard("peach", Card.Heart, 1)
  local card2 = room:printCard("slash", Card.Spade, 1)
  FkTest.runInRoom(function ()
    room:obtainCard(comp2, card2)
    me:pindian({comp2}, xiaoerbianri.name, card)
  end)
  lu.assertEquals(card.number, 1)
end)

return xiaoerbianri